home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DATATYPE.SWG / 0013_VARARRY2.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  1KB  |  47 lines

  1. {
  2. >  I'm trying to figure out a way to declair a Variable, such as an
  3. >  Array, and I don't know the size Until I've loaded the Program.
  4. >  I've tried stuff like........
  5. >  Type
  6. >      Buf : Array[1..1000] of Char;
  7. >  Var
  8. >      Buffer : ^Buf
  9. >  begin
  10. >    Getmem(Buffer,xxx)
  11. }
  12.  
  13. Type
  14.   TElement = LongInt ;     { Here use your own }
  15.  
  16. Const
  17.   MaxElement = 65500 div Sizeof(TElement) ;
  18.  
  19. Type
  20.   TElementArray = Array[1..MaxElement] of TElement ;
  21.   PElementArray = ^TElementArray ;
  22.  
  23. Var
  24.   i    : Word ;
  25.   Elms : PElementArray ;
  26.  
  27. begin
  28.   Write('How many of ''em do you feel like using ? :') ;
  29.   ReadLn(i) ;
  30.   if i>MaxElement then
  31.   begin
  32.     WriteLn('That''s more than I can hold, sorry...') ;
  33.     Halt(1) ;
  34.   end ;
  35.   GetMem(Elms, i*Sizeof(TElement)) ;
  36.  
  37.   { Now, use Elms^[1] to Elms^[i] }
  38.  
  39.   FreeMem(Elms, i*Sizeof(TElement)) ;
  40. end.
  41.  
  42. {
  43. Please note that the previous allows you to keep range checking on, but that
  44. does not garanty you any security : access to an element which's index is
  45. greater than i would cause no RTE, but writing to it will quite mess up things
  46. in memory...
  47. }